import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.Color;
import java.awt.Image;  
import java.awt.Graphics;  
import java.awt.Toolkit;  
import java.util.ArrayList;

public class TheGame extends JPanel implements ActionListener,MouseMotionListener, MouseListener
{
	static Money money = new Money();
	static private int moneyy = money.getMoney();

	//Name says it, the main frame
	  static JFrame mainFrame = new JFrame("Bloons Tower Defense");
	  
	  //Where the buttons and display will go
	  private static JPanel interfacePane = new JPanel();
	  //Display the money earned
	  static JLabel displayMoney= new JLabel("Money: " + moneyy);
	  //Three buttons: Buy Monkey, which will call the monkey class and put one into the JFrame, StartRound which will start the round and Exit which will open a new game of course cause thats just what exit does....
	  static private JButton buyButton = new JButton("Buy Monkey");
	  static private JButton buyButtonCancel = new JButton("Cancel Monkey Buy");
	  static private JButton startRound = new JButton("Start Round");
	  static private JButton endGame = new JButton("Exit");
	  //Image of the backgroundGrass and the balloon
	  private Image backgroundGrass, balloon, monkeyPicture;
	  //Array to store all the bloons on the map
	  private ArrayList<Bloon> bloons = new ArrayList<Bloon>();
	  private ArrayList<Monkey> monkeys = new ArrayList<Monkey>();
	  //Set to true while round is in progress
	  private boolean roundRunning = true;
	  //Max number of bloons
	  private int max = 500;
	  private int XPos = 0, YPos = 0;
	  private Place spotClicked = new Place(0,0);
	  private boolean BuyMonkey = false;
	  
	  static private int testBallonPop = 0;
	  //Variables for the turns for the balloons
	  //static int[] xVars = {0, 140, 140, 272, 272, 88, 88, 527, 527, 380, 380, 528, 528, 331, 331};
	  //static int[] yVars = {354, 354, 123, 123, 518, 518, 639, 639, 434, 434, 254, 254, 54, 54, 0};
	  
	  public TheGame()
	  {
		  //Gets the image
		  BufferedImage img1 = null;
		  BufferedImage img2 = null;
		  BufferedImage img3 = null;

		  addMouseMotionListener(this);
		  addMouseListener(this);
		  
		  try
		  {
			  img1 = ImageIO.read(new File("src//backgroundGrass.gif"));
		  }  catch (IOException e) {
		  }
		  
		  try
		  {
			  img2 = ImageIO.read(new File("src//ballon.gif"));
		  } catch (IOException e) {
		  }
		  try
		  {
			  img3 = ImageIO.read(new File("src//monkey.gif"));
		  } catch (IOException e){
		  }
		  
		  backgroundGrass = img1;
		  balloon = img2;
		  monkeyPicture = img3;
		  
		  //Adds the buyButton
		  buyButton.addActionListener(this);
		  buyButton.setBounds(10, 150, 150, 100);
		  interfacePane.add(buyButton);
		  
		  buyButtonCancel.addActionListener(this);
		  buyButtonCancel.setBounds(10, 25, 150, 100);
		  interfacePane.add(buyButtonCancel);
		  
		  //Adds the startRoundButton
		  startRound.addActionListener(this);
		  startRound.setBounds(10, 500, 150, 100);
		  interfacePane.add(startRound);
		  
		  //Adds the exitButton
		  endGame.addActionListener(this);
		  endGame.setBounds(10, 613, 150, 100);
		  interfacePane.add(endGame);
	  }
	  
	  public void paintComponent(Graphics g)
	  {
		  
		  //Draws image or if it can't find it, draws an error
		  if(backgroundGrass != null)
			  g.drawImage(backgroundGrass, 0, 0, null);
		  else
			  g.drawString("File failed to load", 10, 150);
		  
		  for(Bloon bln : bloons)
		    {
		     for(Monkey monk : monkeys)
		     {
		      if((bln.location.x >= (monk.getX() - monk.getRadius()) && bln.location.x<= (monk.getX() + monk.getRadius())) && ((bln.location.y >= (monk.getY() - monk.getRadius()) && bln.location.y <= (monk.getY() + monk.getRadius()))))
		       {
		       bln.popped = true;
		       }
		     }
		     if(balloon != null && bln.popped == false)
		    	 g.drawImage(balloon, bln.location.x, bln.location.y, null);
		   }

		  for(int i = 0; i< monkeys.size(); i++)
		  {
		  g.drawImage(monkeyPicture, monkeys.get(i).getX(), monkeys.get(i).getY(), null);
		  }
		  updateMoney();
	  }
	  
	  public static void main(String[] args)
	  {
		  //Call constructor
		  TheGame back = new TheGame();
		  mainFrame.setBounds(250, 0, 750, 750);

		  //Set the Interface Pane into place
		  mainFrame.add(interfacePane);
		  interfacePane.setLocation(573, 0);
		  interfacePane.setSize(175, 750);
		  interfacePane.setBackground(Color.GRAY);
		  interfacePane.setLayout(null);
		  
		  //Display the money
		  displayMoney.setBounds(45, 345, 150, 50);
		  interfacePane.add(displayMoney);
		  displayMoney.setFont(new Font("Dialog", Font.PLAIN, 17));
		  
		  //Closes on exit, this prevents multiple instances of the game running at one time in the background
		  mainFrame.getContentPane().add(back);
		  mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		  //Makes it visible and cannot be resized
		  mainFrame.setVisible(true);
		  mainFrame.setResizable(false);
		  
		  back.startRound();
		 
	  }
	  public void mouseMoved(MouseEvent event) 
	  {
		  if(BuyMonkey  == true)
		  {
			  XPos = (int) event.getPoint().getX();
			  YPos = (int) event.getPoint().getY();
		  }
	  }
	  public void mouseDragged(MouseEvent event) 
	  {
		  if(BuyMonkey  == true)
			  mouseMoved(event);
	  }
	  public void startRound()
	  {
		  long time = System.currentTimeMillis();
		  while(roundRunning)
		  {
			  if (System.currentTimeMillis() >= time + 1000 && bloons.size() < max)
			  {
				  bloons.add(new Bloon());
				  time += 1000;
			  }
			  moveAll();
		  }
	  }
	  
	  public void moveAll()
	  {
		  for(Bloon b : bloons)
		  {
			  b.move();
		  }
		  repaint();
		  try{
			  	Thread.sleep(7);
		  }catch(InterruptedException e) {}
	  }
	  
	  public void updateMoney()
	  {
		  displayMoney.setText("Money: " + moneyy);
		  revalidate();
	  }
	  
	  public void actionPerformed(ActionEvent event)
	  {
		  String command = event.getActionCommand();
		  int input = 0;
		  
		  if(command == "Exit")
		  {
			  input = JOptionPane.showConfirmDialog(null, "Are you sure you want to close the game?");
			  if(input == JOptionPane.YES_OPTION)
				 mainFrame.dispose();
		  }
		  else if (command == "Start Round")
		  {
			  System.out.println("Call start Round class");
		  }
		  else if(command == "Buy Monkey")
		  {
			  BuyMonkey= true;
			  System.out.println("Buying Monkey");
		  }
		  else if(command == "Cancel Monkey Buy")
		  {
			  BuyMonkey= false;
			  System.out.println("Canceling Monkey");
		  }
	  }
	  public void mouseClicked(MouseEvent e)
	  {
		  if(moneyy >= 1 && BuyMonkey  == true)
		  {
				  System.out.println("click : "+XPos+" "+YPos);
				  BuyMonkey = false;
				  System.out.println("Purchase Completed");
				  Monkey MonkeyNew = new Monkey(XPos-25,YPos-25,75,1,1);
				  monkeys.add(MonkeyNew);
				  moneyy--;
				  System.out.println(moneyy);
				  repaint();
				  updateMoney();
			  
		  }
		  else
			  System.out.println("Not enough Money");
	  }

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}